#!/bin/bash

if [[ -z "$AWK" || -z "$WORKDIR" ]]; then
    printf '$AWK and $WORKDIR must be set\n' >&2
    exit 1
fi

TEMP1=$WORKDIR/test.temp.1
TEMP2=$WORKDIR/test.temp.2

RESULT=0

fail() {
	echo "$1" >&2
	RESULT=1
}

echo T.recache: test re cache in b.c
	# thanks to ross ridge for this horror

echo b > $TEMP1
$AWK '
BEGIN {
        #
        # Fill up DFA cache with run-time REs that have all been
        # used twice.
        #
        CACHE_SIZE=64
        for(i = 0; i < CACHE_SIZE; i++) {
                for(j = 0; j < 2; j++) {
                        "" ~ i "";
                }
        }
        #
        # Now evalutate an expression that uses two run-time REs
        # that have never been used before.  The second RE will
        # push the first out of the cache while the first RE is 
        # still needed.
        #
        x = "a"
        reg1 = "[Aa]"
        reg2 = "A"
        sub(reg1, x ~ reg2 ? "B" : "b", x)

        print x
}
' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.recache'

exit $RESULT
